﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Portal.Nhibernate.Repository;

namespace Portal.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 广告模块
    /// </summary>
    [Description("广告模块")]
    public class AdvertisementController : BaseAdminController
    {
        private readonly IAdvertisementRepository<IAdvertisement> _advertisementRepository;
        public AdvertisementController(IAdvertisementRepository<IAdvertisement> advertisementRepository)
        {
            _advertisementRepository = advertisementRepository;
        }
        public AdvertisementController()
        {
            _advertisementRepository = new AdvertisementRepository();
        }
        /// <summary>
        /// 广告管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            _advertisementRepository.Get();
            return View();
        }
        /// <summary>
        /// 获取广告管理列表JSON（老版本兼容）
        /// </summary>
        /// <param name="sort">排序字段</param>
        /// <param name="order">排序方式</param>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页记录条数</param>
        /// <returns>返回广告json列表</returns>
        public ActionResult GetAdvertisementJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_advertisementRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
        /// <summary>
        /// 获取广告管理列表JSON
        /// </summary>
        /// <param name="sort">排序字段</param>
        /// <param name="order">排序方式</param>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页记录条数</param>
        /// <returns>返回广告json列表</returns>
        public ActionResult GetListJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_advertisementRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
        /// <summary>
        /// 创建广告页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {

            var model = _advertisementRepository.CreateEntity();
            if (Request.IsAjaxRequest())
                return View("CreateControl", model);
            return View(model);
        }
        /// <summary>
        /// 创建保存广告
        /// </summary>
        /// <param name="formCollection"></param>
        /// <param name="entity">接收数据实体</param>
        /// <param name="advertisementImage">广告图片</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.AdvertisementList)]
        public ActionResult Create(FormCollection formCollection, Advertisement entity, HttpPostedFileBase advertisementImage)
        {

            var model = _advertisementRepository.CreateEntity();
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.CategoryId = entity.CategoryId;
            model.Name = entity.Name;
            model.Sort = entity.Sort;
            if (advertisementImage != null)
            {
                var upload = new UploadHelper(advertisementImage, "/Upload/Advertisement/");
                upload.Save();
                model.Url = upload.FileRelativePath;
            }
            _advertisementRepository.Add(model);
            _advertisementRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "添加成功", state = true };
            if (Request.IsAjaxRequest())
                return Json(new AjaxResult { message = "添加成功", state = true });
            return View(model);
        }
        /// <summary>
        /// 编辑广告页面
        /// </summary>
        /// <param name="id">要编辑广告ID</param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            var model = _advertisementRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
            {
                if (Request.IsAjaxRequest())
                    return Json(new AjaxResult { message = "没有找到记录", state = false });
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            }
            if (Request.IsAjaxRequest())
                return View("EditControl", model);
            return View(model);
        }
        /// <summary>
        /// 编辑保存广告
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="advertisementImage"></param>
        /// <returns></returns>
        [HttpPost]
        [CacheResetFilterAttribute(CacheKey.AdvertisementList)]
        public ActionResult Edit(Advertisement entity, HttpPostedFileBase advertisementImage)
        {
            var model = _advertisementRepository.Get().SingleOrDefault(o => o.Id == entity.Id);
            if (model == null)
            {
                if (Request.IsAjaxRequest())
                    return Json(new AjaxResult { message = "没有找到记录", state = false });
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });

            }
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.CategoryId = entity.CategoryId;
            model.Name = entity.Name;
            model.Sort = entity.Sort;
            if (advertisementImage != null)
            {
                var upload = new UploadHelper();
                upload.ReplaceFile(advertisementImage, "/Upload/Advertisement/", model.Url);
                model.Url = upload.FileRelativePath;
            }
            _advertisementRepository.Update(model);
            _advertisementRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "没有找到记录", state = true };
            if (Request.IsAjaxRequest())
                return Json(new AjaxResult { message = "没有找到记录", state = true });
            return View(model);
        }
        /// <summary>
        /// 删除广告
        /// </summary>
        /// <param name="id">要删除广告ID</param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        [CacheResetFilterAttribute(CacheKey.AdvertisementList)]
        public ActionResult Delete(List<string> id, FormCollection collection)
        {
            var list = _advertisementRepository.Get().Where(o => id.Contains(o.Id));
            list.ForEach(o => _advertisementRepository.Delete(o));
            _advertisementRepository.Save();
            return Json(new AjaxResult { state = true, message = "删除成功" });
        }
    }
}
